﻿using System;
using UnityEngine;

namespace UnityModule
{
    //资源管理器
    public sealed partial class ResourceComponent : SingletonBehaviour<ResourceComponent>
    {
        private Action m_onInitFinish;
        private Action m_onPreloadFinish;

        private Action m_lowMemory;
        public event Action LowMemory
        {
            add
            {
                m_lowMemory -= value;
                m_lowMemory += value;
            }
            remove
            {
                m_lowMemory -= value;
            }
        }

        //初始化
        public void Init(Action onInitFinish)
        {
            m_onInitFinish = onInitFinish;        
            Init_Internal();
        }

        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="abName">assetbundle名称</param>
        /// <param name="resName">资源名称</param>
        /// <param name="callBack">回调</param>
        /// <param name="options">加载选项</param>
        public LoadTask LoadResource(string abName, string resName, Action<LoadedRes> callBack, LoadOptions options = LoadOptions.Auto)
        {
            abName = abName.ToLower();
            LoadedRes cachedRes = GetCachedRes(abName, resName);
            if (null != cachedRes)
            {
                cachedRes.lastUseTime = Time.realtimeSinceStartup;
                if (null != callBack) callBack(cachedRes);
                return null;
            }
            LoadTask loadTask = new LoadTask();
#if UNITY_EDITOR
            if (UpdateSettings.GameSetting.emResType == ResourceMode.EditorLocalRes)
            {
                GetResForSimulation(abName, resName, (loadedRes) =>
                {
                    if (null != callBack && !loadTask.IsDispose)
                    {
                        loadTask.Dispose();
                        callBack(loadedRes);
                    }
                }, options);
            }
            else
#endif
            {
                GetRes(abName, resName, (loadedRes) =>
                {
                    if (null != callBack && !loadTask.IsDispose)
                    {
                        loadTask.Dispose();
                        callBack(loadedRes);
                    }
                }, options);
            }
            return loadTask;
        }

   
        
        public void UnloadSceneOnlyAndAutoRes()
        {
            AssetBundleCom.UnloadSceneOnlyBundle();
            FreeMemory();
        }

        /// <summary>
        /// 卸载bundle,现在统一用lz4格式bundle，bundle可以不卸载，如果一定知道不用了可以 bUnloadAssetBundle = true 就会卸载bundle
        /// </summary>
        /// <param name="abNameWithoutHash"></param>
        public void UnloadRes(string abName, string resName, bool bUnloadAssetBundle = false,bool unloadType = false)
        {
            string key = abName + "-" + resName;
            if (dicRes.ContainsKey(key))
                dicRes.Remove(key);

            if(bUnloadAssetBundle)
                AssetBundleCom.Unload(abName, unloadType);
        }

        public void UnloadResNew(string abName, string resName, bool bUnloadAssetBundle = false, bool unloadType = false)
        {
            string key = abName + "-" + resName;
            if (dicRes.ContainsKey(key))
                dicRes.Remove(key);

            if (bUnloadAssetBundle)
                AssetBundleCom.Unload(abName, unloadType);
        }
    }
}
